import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    shade: false,
    objectArray: [],
    arrIndex: null,
    materialTypeId: '',
    search: '',
    list: [],
  },
  onLoad() {
    this.getMateDataRecordQuery()
    this.mateDataMaterialQuery()
  },

  mateDataMaterialQuery() {
    let that = this
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    let form = prevPage.data.form
    $http(apiSetting.mateDataMaterialQuery, {
      materialTypeId: that.data.materialTypeId,
      search: that.data.search,
    }).then((data) => {
      let arr = data.result
      for (let i in arr) { }
      let find = arr.findIndex(n => {
        return n.code == form.materialCode
      })
      if (find != -1) {
        arr[find].checked = true
      }
      that.setData({
        list: arr
      })
    })
  },

  // 物料
  getMateDataRecordQuery() {
    let that = this
    // const pages = getCurrentPages();
    // let currPage = pages[pages.length - 1]; //当前页面
    // let prevPage = pages[pages.length - 2]; //上一个页面
    // let form = prevPage.data.form
    // $http(apiSetting.mateDataRecordQuery, {
    //   metaDataName: "物料", //定值：人员 物料 生产设备
    // }).then((data) => {
    //   let arr = data.result
    //   for (let i in arr) { }
    //   let find = arr.findIndex(n => {
    //     return n.code == form.materialCode
    //   })
    //   if (find != -1) {
    //     arr[find].checked = true
    //   }
    //   that.setData({
    //     list: arr
    //   })
    // })
    $http(apiSetting.mateDataRecordQuery, {
      metaDataName: "物料类型", //定值：人员 物料 生产设备
    }).then((data) => {
      let arr = [{
        name: "全部",
        code: ""
      }, ...data.result]
      console.log(arr)
      that.setData({
        arrIndex: 0,
        objectArray: arr
      })
    })
  },

  bindObjPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let val = e.detail.value
    this.setData({
      arrIndex: val
    });
    this.data.materialTypeId = this.data.objectArray[val].dap_id_
    this.mateDataMaterialQuery()
  },

  radioChange(e) {
    let ind = e.target.dataset.index
    for (let i in this.data.list) {
      this.data.list[i].checked = false
    }
    this.data.list[ind].checked = true
    this.setData({
      list: this.data.list
    })
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.data.getMaterial = true
    prevPage.data.materialData = this.data.list[ind]
    dd.navigateBack({
      delta: 1
    })
  },

  onSearch(e) {
    let value = e.detail.value
    this.data.search = value
  },

  searchBtn() {
    this.mateDataMaterialQuery()
  }

});
